//引入express模块
const express = require("express");
//引入数据库连接模块
const pool = require("../pool.js");
//引入公用函数
const xsp_fn = require("../others/xsp_fn");
//创建路由器对象
const mR = express.Router();
//以下为接口部分

//订单添加接口
//接口地址：http://127.0.0.1:3000/order/add
//接口方式：post
mR.post("/add", (req, res, next) => {
  let c_add = xsp_fn.api_add("order_info");
  pool.query(c_add.sql, [req.body], (err, data) => {
    if (err) {
      res.send(c_add.err);
      next(err);
    } else {
      console.log(data);
      res.send(c_add.success);
    }
  });
});

//删除订单接口
//接口地址：http://127.0.0.1:3000/ping/delete
//接口方式：delete
mR.delete("/delete", (req, res, next) => {
  let c_delete = `delete from order_info where order_id=?`;
  pool.query(c_delete, [req.query.order_id], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.changedRows) {
      res.send({ code: 0, msg: "删除失败" });
    } else {
      res.send({ code: 1, msg: "删除成功" });
    }
  });
});

//修改订单接口
//接口地址：http://127.0.0.1:3000/order/update
//接口方式：put
mR.put("/update", (req, res, next) => {
  let ss = req.body.appraise_id;
  console.log(ss);
  let c_update = `update order_info set ? where order_id=?`;
  pool.query(c_update, [req.body, req.body.order_id], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.changedRows) {
      res.send({ code: 1, msg: "修改成功" });
    } else {
      res.send({ code: 0, msg: "修改失败" });
    }
  });
});

//查看用户订单
//接口地址：http://127.0.0.1:3000/order/userinfo
//接口方式：get
mR.get("/userinfo", (req, res, next) => {
  // console.log(req.query.uid);
  pool.query(
    "select * from order_info where user_id=?",
    [req.query.user_id],
    (err, data) => {
      if (err) {
        console.log(err);
        next(err);
        return;
      }
      res.send({ code: 1, msg: "查询成功", data });
    }
  );
});

//订单状态分页查询接口
//2.1接口地址：http://127.0.0.1:3000/order/list?trade_status=(状态)&user_id=(用户id)&pno=(显示第几页)&pageSize=(一页显示几条数据)
mR.get("/list", (req, res, next) => {
  let { pno = 1, pageSize = 10 } = req.query;
  let start = (pno - 1) * pageSize;
  let sql1 = `select * from order_info where trade_status=? && user_id=?`;
  pool.query(
    sql1,
    [req.query.trade_status, req.query.user_id],
    (err, data1) => {
      if (err) {
        console.log(err);
        next(err);
        return;
      }
      recordCount = data1.length;
      pageCount = Math.ceil(recordCount / pageSize);
      let sql2 = `select * from order_info where trade_status=${req.query.trade_status} && user_id=${req.query.user_id} limit ${start},${pageSize}`;

      pool.query(sql2, (err, data) => {
        if (err) {
          console.log(err);
          next(err);
          return;
        }
        if (data[0]) {
          res.send({
            code: 1,
            msg: "查询成功",
            pno,
            pageSize,
            pageCount,
            recordCount,
            data,
          });
        } else {
          res.send({ code: 0, msg: "查询失败", data });
        }
      });
    }
  );
});

//订单状态分页查询接口
//2.1接口地址：http://127.0.0.1:3000/order/fenlist?trade_status=(状态)&pno=(显示第几页)&pageSize=(一页显示几条数据)
mR.get("/fenlist", (req, res, next) => {
  let { pno = 1, pageSize = 10 } = req.query;
  let start = (pno - 1) * pageSize;
  let sql1 = `select * from order_info where trade_status=?`;
  pool.query(sql1, [req.query.trade_status], (err, data1) => {
    if (err) {
      console.log(err);
      next(err);
      return;
    }
    recordCount = data1.length;
    pageCount = Math.ceil(recordCount / pageSize);
    let sql2 = `select * from order_info where trade_status=${req.query.trade_status} limit ${start},${pageSize}`;

    pool.query(sql2, (err, data) => {
      if (err) {
        console.log(err);
        next(err);
        return;
      }
      if (data[0]) {
        res.send({
          code: 1,
          msg: "查询成功",
          pno,
          pageSize,
          pageCount,
          recordCount,
          data,
        });
      } else {
        res.send({ code: 0, msg: "查询失败", data });
      }
    });
  });
});

//暴露出去
module.exports = mR;
